﻿<h2>String</h2>
<p id="validate-string-initially-blank">
    Initially blank:
    <Validation Validator="@ValidateStringSelect">
        <Select TValue="string">
            <ChildContent>
                <SelectItem Value="string.Empty"></SelectItem>
                <SelectItem Value="@("Oliver")">Oliver</SelectItem>
                <SelectItem Value="@("Harry")">Harry</SelectItem>
                <SelectItem Value="@("Jack")">Jack</SelectItem>
                <SelectItem Value="@("George")">George</SelectItem>
            </ChildContent>
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </Select>
    </Validation>
</p>

<h2>String with bind</h2>
<p id="validate-string-with-bind-initially-blank">
    Initially blank:
    <Validation Validator="@ValidateStringSelect">
        <Select @bind-SelectedValue="@stringWithBindInitiallyBlank">
            <ChildContent>
                <SelectItem Value="string.Empty"></SelectItem>
                <SelectItem Value="@("Oliver")">Oliver</SelectItem>
                <SelectItem Value="@("Harry")">Harry</SelectItem>
                <SelectItem Value="@("Jack")">Jack</SelectItem>
                <SelectItem Value="@("George")">George</SelectItem>
            </ChildContent>
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </Select>
    </Validation>
</p>
<p id="validate-string-with-bind-initially-selected">
    Initially selected:
    <Validation Validator="@ValidateStringSelect">
        <Select @bind-SelectedValue="@stringWithBindInitiallySelected">
            <ChildContent>
                <SelectItem Value="string.Empty"></SelectItem>
                <SelectItem Value="@("Oliver")">Oliver</SelectItem>
                <SelectItem Value="@("Harry")">Harry</SelectItem>
                <SelectItem Value="@("Jack")">Jack</SelectItem>
                <SelectItem Value="@("George")">George</SelectItem>
            </ChildContent>
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </Select>
    </Validation>
</p>

<h2>String with event</h2>
<p id="validate-string-with-event-initially-blank">
    Initially blank:
    <Validation Validator="@ValidateStringSelect">
        <Select TValue="string" SelectedValue="@stringWithEventInitiallyBlank" SelectedValueChanged="@((v)=> { stringWithEventInitiallyBlank = v; })">
            <ChildContent>
                <SelectItem Value="string.Empty"></SelectItem>
                <SelectItem Value="@("Oliver")">Oliver</SelectItem>
                <SelectItem Value="@("Harry")">Harry</SelectItem>
                <SelectItem Value="@("Jack")">Jack</SelectItem>
                <SelectItem Value="@("George")">George</SelectItem>
            </ChildContent>
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </Select>
    </Validation>
</p>
<p id="validate-string-with-event-initially-selected">
    Initially selected:
    <Validation Validator="@ValidateStringSelect">
        <Select TValue="string" SelectedValue="@stringWithEventInitiallySelected" SelectedValueChanged="@((v)=> { stringWithEventInitiallySelected = v; })">
            <ChildContent>
                <SelectItem Value="string.Empty"></SelectItem>
                <SelectItem Value="@("Oliver")">Oliver</SelectItem>
                <SelectItem Value="@("Harry")">Harry</SelectItem>
                <SelectItem Value="@("Jack")">Jack</SelectItem>
                <SelectItem Value="@("George")">George</SelectItem>
            </ChildContent>
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </Select>
    </Validation>
</p>

<h2>Int</h2>
<p id="validate-int-initially-blank">
    <Validation Validator="@ValidateIntSelect">
        <Select TValue="int">
            <ChildContent>
                <SelectItem Value="0"></SelectItem>
                <SelectItem Value="1">1</SelectItem>
                <SelectItem Value="2">2</SelectItem>
                <SelectItem Value="3">3</SelectItem>
                <SelectItem Value="4">4</SelectItem>
            </ChildContent>
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </Select>
    </Validation>
</p>

<h2>Int with bind</h2>
<p id="validate-int-with-bind-initially-blank">
    Initially blank:
    <Validation Validator="@ValidateIntSelect">
        <Select @bind-SelectedValue="@intWithBindInitiallyBlank">
            <ChildContent>
                <SelectItem Value="0"></SelectItem>
                <SelectItem Value="1">1</SelectItem>
                <SelectItem Value="2">2</SelectItem>
                <SelectItem Value="3">3</SelectItem>
                <SelectItem Value="4">4</SelectItem>
            </ChildContent>
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </Select>
    </Validation>
</p>
<p id="validate-int-with-bind-initially-selected">
    Initially selected:
    <Validation Validator="@ValidateIntSelect">
        <Select @bind-SelectedValue="@intWithBindInitiallySelected">
            <ChildContent>
                <SelectItem Value="0"></SelectItem>
                <SelectItem Value="1">1</SelectItem>
                <SelectItem Value="2">2</SelectItem>
                <SelectItem Value="3">3</SelectItem>
                <SelectItem Value="4">4</SelectItem>
            </ChildContent>
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </Select>
    </Validation>
</p>

<h2>Multi string</h2>
<p id="validate-multi-string-initially-blank">
    Initially blank:
    <Validation Validator="@ValidateMultiStringSelect">
        <Select @ref="selectMultipleStrings" TValue="string" Multiple>
            <ChildContent>
                <SelectItem Value="@("Oliver")">Oliver</SelectItem>
                <SelectItem Value="@("Harry")">Harry</SelectItem>
                <SelectItem Value="@("Jack")">Jack</SelectItem>
                <SelectItem Value="@("George")">George</SelectItem>
            </ChildContent>
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </Select>
    </Validation>
</p>

<h2>Enum</h2>
<p id="validate-enum-initially-blank">
    <Validation Validator="@ValidateEnumSelect">
        <Select TValue="SelectableValues">
            <ChildContent>
                <SelectItem Value="SelectableValues.None"></SelectItem>
                <SelectItem Value="SelectableValues.First">First</SelectItem>
                <SelectItem Value="SelectableValues.Second">Second</SelectItem>
                <SelectItem Value="SelectableValues.Last">Last</SelectItem>
            </ChildContent>
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </Select>
    </Validation>
</p>

<h2>Enum with bind</h2>
<p id="validate-enum-with-bind-initially-blank">
    Initially blank:
    <Validation Validator="@ValidateEnumSelect">
        <Select @bind-SelectedValue="@enumWithBindInitiallyBlank">
            <ChildContent>
                <SelectItem Value="SelectableValues.None"></SelectItem>
                <SelectItem Value="SelectableValues.First">First</SelectItem>
                <SelectItem Value="SelectableValues.Second">Second</SelectItem>
                <SelectItem Value="SelectableValues.Last">Last</SelectItem>
            </ChildContent>
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </Select>
    </Validation>
</p>
<p id="validate-enum-with-bind-initially-selected">
    Initially selected:
    <Validation Validator="@ValidateEnumSelect">
        <Select @bind-SelectedValue="@enumWithBindInitiallySelected">
            <ChildContent>
                <SelectItem Value="SelectableValues.None"></SelectItem>
                <SelectItem Value="SelectableValues.First">First</SelectItem>
                <SelectItem Value="SelectableValues.Second">Second</SelectItem>
                <SelectItem Value="SelectableValues.Last">Last</SelectItem>
            </ChildContent>
            <Feedback>
                <ValidationError>error</ValidationError>
            </Feedback>
        </Select>
    </Validation>
</p>
@code{
    void ValidateStringSelect( ValidatorEventArgs e )
    {
        var selectedValue = e.Value as string;
        e.Status = !string.IsNullOrEmpty( selectedValue ) ? ValidationStatus.Success : ValidationStatus.Error;
    }

    void ValidateMultiStringSelect( ValidatorEventArgs e )
    {
        var selectedValue = e.Value as string[];
        e.Status = selectedValue?.Length > 0 ? ValidationStatus.Success : ValidationStatus.Error;
    }

    void ValidateIntSelect( ValidatorEventArgs e )
    {
        var selectedValue = Convert.ToInt32( e.Value );
        e.Status = selectedValue != 0 ? ValidationStatus.Success : ValidationStatus.Error;
    }

    void ValidateEnumSelect( ValidatorEventArgs e )
    {
        var selectedValue = e.Value is SelectableValues ? (SelectableValues)e.Value : SelectableValues.None;
        e.Status = selectedValue != SelectableValues.None ? ValidationStatus.Success : ValidationStatus.Error;
    }

    string stringWithBindInitiallyBlank = string.Empty;
    string stringWithBindInitiallySelected = "Jack";

    string stringWithEventInitiallyBlank = string.Empty;
    string stringWithEventInitiallySelected = "Oliver";

    int intWithBindInitiallyBlank;
    int intWithBindInitiallySelected = 1;

    public Select<string> selectMultipleStrings;

    enum SelectableValues { None, First, Second, Last };
    SelectableValues enumWithBindInitiallyBlank;
    SelectableValues enumWithBindInitiallySelected = SelectableValues.First;
}